package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.Type;
import pers.cj.pdos.entity.Unit;
import pers.cj.pdos.service.ITypeService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.UuidUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/goods")
public class TypeController extends BaseController {
    @Autowired
    private ITypeService typeService;

    @RequiresPermissions("type:query")
    @RequestMapping("/type")
    public String type(){
        return "goods/type";
    }

    @ResponseBody
    @RequiresPermissions("type:query")
    @RequestMapping("/type/getList")
    public ResponseList getList(int page, int limit){
        Company company = getCompany();
        List<Type> list = typeService.listPage(page,limit,company.getId());
        return ResponseList.success(typeService.count(company.getId()), list);
    }

    @ResponseBody
    @RequiresPermissions("type:insert")
    @RequestMapping("/type/add")
    public ResponseEntity add(@RequestBody Type type) {
        Company company = getCompany();
        if (typeService.checkName(type.getName(),company.getId())){
            return new ResponseEntity(2,"分类名称已存在");
        }
        DataUtil.processEntity(company, type);
        typeService.insert(type);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("type:delete")
    @RequestMapping("/type/delete")
    public ResponseEntity delete(@RequestBody Type type) {
        try {
            DataUtil.processEntity(false,getCompany(),type);
            typeService.remove(type);
            return ResponseEntity.success();
        } catch (Exception e) {
            return ResponseEntity.fail("已被使用无法删除");
        }
    }

    @ResponseBody
    @RequiresPermissions("type:insert")
    @RequestMapping("/type/update")
    public ResponseData update(@RequestBody Type type, Boolean checkName) {
        Company company = getCompany();
        if (checkName && typeService.checkName(type.getName(),company.getId())){
            return ResponseData.fail("分类名称已存在");
        }
        DataUtil.processEntity(false,getCompany(),type);
        typeService.update(type);

        return ResponseData.success(type);
    }

    @ResponseBody
    @RequestMapping("/type/getAll")
    public ResponseData getAll() {
        Company company = getCompany();
        List<Type> typeList = typeService.list(company.getId());
        return ResponseData.success(typeList);
    }
}
